package com.example.administrator.ygddriver;

import android.util.Log;
import android.view.View;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.example.administrator.ygddriver.base.BaseFragment;
import com.example.administrator.ygddriver.map.SendMapActivity;

public class MapFragment extends BaseFragment {
    private MapView mMapView;
    private MyLocationListener myListener = new MyLocationListener();
    public LocationClient mLocationClient = null;
    private BaiduMap mBaiduMap;

    @Override
    public int setLayout() {
        return R.layout.fragment_map;
    }

    @Override
    public void initView(View view) {
        mMapView = view.findViewById(R.id.fragment_map);
        mBaiduMap = mMapView.getMap();
    }

    @Override
    public void initData() {
        mLocationClient = new LocationClient(context);
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        setLocationClientOption();
        mLocationClient.start();
        // 不显示地图上比例尺
        mMapView.showScaleControl(false);

        // 不显示地图缩放控件（按钮控制栏）
        mMapView.showZoomControls(false);
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            int errorCode = location.getLocType();
            Log.d("111111", "errorCode:" + errorCode);
            if (errorCode == 161 || errorCode == 61) {
                mBaiduMap.clear();
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.mipmap.car);

                //构建MarkerOption，用于在地图上添加Marker
                LatLng latLng = new LatLng( location.getLatitude(),location.getLongitude());

                OverlayOptions option = new MarkerOptions()
                        .position(latLng)
                        .icon(bitmap);

                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                builder.include(latLng);
                mBaiduMap.setMapStatus(MapStatusUpdateFactory
                        .newLatLngBounds(builder.build()));
                mBaiduMap.addOverlay(option);
            }

        }
    }

    private void setLocationClientOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(20000);
        option.setOpenGps(true);
        option.setLocationNotify(true);
        option.setIgnoreKillProcess(false);
        option.SetIgnoreCacheException(false);
        option.setEnableSimulateGps(false);
        mLocationClient.setLocOption(option);
        option.setIsNeedAddress(true);
        option.setIsNeedLocationPoiList(true);

    }


    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时必须调用mMapView. onResume ()
        if (mMapView != null) {
            mMapView.onResume();
        }
        mLocationClient.start();

    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时必须调用mMapView. onPause ()
        if (mMapView != null) {
            mMapView.onPause();
        }
        mLocationClient.stop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时必须调用mMapView.onDestroy()
        if (mMapView != null) {
            mMapView.onDestroy();
        }
        mLocationClient.stop();
    }
}
